package org.example.controller.workflow;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/workflow")
public class WorkflowController {

    @Autowired
    RuntimeService runtimeService;
    @Autowired
    TaskService taskService;
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    ProcessEngine processEngine;

    @PostMapping("/deploy")
    public Object deploy(){
        Map<String,Object> result = new HashMap<>();




        return result;
    }


    @PostMapping(value = "/start",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object start(@RequestBody JSONObject body){
        log.info("requestBody={}",body);
        String definitionKey = body.getStr("definitionKey");
        Map<String,Object> variables = body.getJSONObject("variables");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(definitionKey, variables);

        JSONObject result = new JSONObject(true)
                .set("getId",processInstance.getId())
                .set("getName",processInstance.getName())
                .set("getProcessInstanceId",processInstance.getProcessInstanceId())
                .set("getProcessVariables",processInstance.getProcessVariables())
                .set("getBusinessKey",processInstance.getBusinessKey())
                .set("getProcessDefinitionId",processInstance.getProcessDefinitionId())
                .set("getProcessDefinitionKey",processInstance.getProcessDefinitionKey())
                .set("getProcessDefinitionName",processInstance.getProcessDefinitionName())
                .set("getProcessDefinitionVersion",processInstance.getProcessDefinitionVersion())
                .set("getDeploymentId",processInstance.getDeploymentId())
                .set("getDescription",processInstance.getDescription())
                .set("getLocalizedDescription",processInstance.getLocalizedDescription())
                .set("getLocalizedName",processInstance.getLocalizedName())
                .set("getCallbackId",processInstance.getCallbackId())
                .set("getCallbackType",processInstance.getCallbackType())
                .set("getSuperExecutionId",processInstance.getSuperExecutionId())
                .set("getStartTime",processInstance.getStartTime())
                .set("getStartUserId",processInstance.getStartUserId())
                .set("getReferenceId",processInstance.getReferenceId())
                .set("getReferenceType",processInstance.getReferenceType())
                .set("getPropagatedStageInstanceId",processInstance.getPropagatedStageInstanceId())
                .set("getRootProcessInstanceId",processInstance.getRootProcessInstanceId())
                .set("getParentId",processInstance.getParentId())
                .set("getActivityId",processInstance.getActivityId())
                .set("getTenantId",processInstance.getTenantId());

        return result;
    }

    @PostMapping(value = "/todo",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object todo(@RequestBody JSONObject body){

        String assignee = body.getStr("assignee");

        List<Task> list = taskService.createTaskQuery()
                .taskAssignee(assignee)
//                .or().taskCandidateUser()
//                .or().taskCandidateGroupIn()
                .orderByTaskId().desc()
                .list();

        JSONObject result = new JSONObject(true);
        result.set("todoList",list);

        return result;
    }

    @PostMapping(value="/submit",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object submit(@RequestBody JSONObject body){
        String taskId = body.getStr("taskId");
        Map<String,Object> variables = body.getJSONObject("variables");

        taskService.complete(taskId, variables, true);

        JSONObject result = new JSONObject(true);
        result.set("success",true);
        return result;
    }

    @PostMapping(value="/rollback",consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Object rollback(@RequestBody JSONObject body){
        String processInstanceId = body.getStr("processInstanceId");
        String currentTaskKey = body.getStr("currentTaskKey");
        String targetTaskKey = body.getStr("targetTaskKey");

        runtimeService.createChangeActivityStateBuilder()
                .processInstanceId(processInstanceId)
                .moveActivityIdTo(currentTaskKey, targetTaskKey)
                .changeState();

        JSONObject result = new JSONObject(true);
        result.set("success",true);
        return result;
    }



}
